package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/damageListGoods")
public class DamageListGoodsController {
    @Autowired
    private DamageListGoodsService damageListGoodsService;

    @PostMapping("/save")
    public ServiceVO save(HttpServletRequest request, DamageList damageList, String damageListGoodsStr, @RequestParam(required = false) String damageNumber) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        damageList.setDamageNumber(damageNumber);
        damageList.setUserId(user.getUserId());
        List<DamageListGoods> damageListGoods = JSON.parseArray(damageListGoodsStr, DamageListGoods.class);
        damageListGoodsService.save(damageList,damageListGoods);
        return new ServiceVO(100, "成功");
    }

    @PostMapping("/list")
    public Map<String,Object>  list(String  sTime, String  eTime){
        return damageListGoodsService.list(sTime,eTime);
    }

    @PostMapping("/goodsList")
    public Map<String,Object>  goodsList(Integer damageListId){
        return damageListGoodsService.goodsList(damageListId);
    }
}
